package com.dingli.shop.controller.controller;

import com.dingli.shop.biz.OrderBiz;
import com.dingli.shop.po.OrderVO;
import com.dingli.shop.vo.JsonVo;
import com.dingli.shop.vo.Meta;
import com.dingli.shop.vo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderBiz orderBiz;
    @GetMapping("/orders")
    public JsonVo orders(@RequestParam Integer pagenum,
                         @RequestParam Integer pagesize) {
        int fromID=(pagenum-1)*pagesize;
        int toID=pagesize;
        Map<String,Integer> map=new HashMap<>();
        map.put("fromID",fromID);
        map.put("toID",toID);
        List<OrderVO> orderVO =  orderBiz.queryOrder(map);
        int total=orderBiz.numOrder();
        Order order=new Order();
        Meta meta = new Meta();
        if (orderVO == null) {
            meta.setMsg("获取订单失败");
            meta.setStatus(400);
        } else {
            meta.setMsg("获取成功");
            meta.setStatus(200);
        }
        order.setGoods(orderVO);
        order.setPagenum(pagenum);
        order.setTotal(total);
        JsonVo jsonVo = new JsonVo();
        jsonVo.setData(order);
        jsonVo.setMeta(meta);
        return jsonVo;
    }
}
